/*reducer 是纯方法
* 传入旧状态和action
* 返回新状态
*/

import { combineReducers } from "redux";
import * as actions from "../actions/index";

// 待办项响应
const todos = (state = [], action) => {
    switch (action.type) {
        case actions.ADD_TODO:
            return [
                // 使用扩展运算符（...）拷贝数组。
                ...state,
                {
                    text: action.text,
                    completed: false
                }
            ]
        case actions.COMPLETE_TODO:
            return [
                ...state.slice(0, action.index),//点击元素之前数组
                Object.assign({}, state[action.index], {
                    completed: true
                }),//点击元素
                ...state.slice(action.index + 1)//点击元素到数组结尾的元素
            ]
        default:
            return state;
    }
}


// 设置过滤器初始值
const { SHOW_ALL } = actions.VisibilityFilters

// 过滤器响应
const visibilityFilter = (state = SHOW_ALL, action) => {
    switch (action.type) {
        case actions.SET_VISIBILITY_FILTER:
            return action.filter
        default:
            return state
    }
}

// 将不同响应合并成一个Reducer

const rootReducer = combineReducers({
    todos,
    visibilityFilter
})
export default rootReducer;